<template>
  <div class="layer-panel">
    <div class="layer-header">
      <div class="layer-title">图层</div>
      <div class="layer-actions">
        <button @click="$emit('add-layer')" title="添加图层" class="action-btn">
          <span>+</span>
        </button>
        <button 
          @click="$emit('delete-layer')" 
          :disabled="!activeLayerId"
          title="删除图层"
          class="action-btn"
        >
          <span>-</span>
        </button>
      </div>
    </div>
    
    <div class="layer-list">
      <div 
        v-for="layer in layers" 
        :key="layer.id"
        class="layer-item"
        :class="{ active: layer.id === activeLayerId }"
        @click="$emit('select-layer', layer.id)"
      >
        <div class="layer-item-header">
          <div class="layer-controls">
            <button 
              class="visibility-toggle"
              @click.stop="$emit('toggle-visibility', layer.id)"
              :title="layer.visible ? '隐藏图层' : '显示图层'"
            >
              <svg 
                class="icon" 
                :class="{ visible: layer.visible }"
                viewBox="0 0 24 24" 
                width="14" 
                height="14"
              >
                <path v-if="layer.visible" d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>
                <path v-else d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z"/>
              </svg>
            </button>
            <button 
              class="lock-toggle"
              @click.stop="$emit('toggle-lock', layer.id)"
              :title="layer.locked ? '解锁图层' : '锁定图层'"
            >
              <svg 
                class="icon" 
                :class="{ locked: layer.locked }"
                viewBox="0 0 24 24" 
                width="14" 
                height="14"
              >
                <path v-if="layer.locked" d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z"/>
                <path v-else d="M12 17c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm6-9h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6h1.9c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm0 12H6V10h12v10z"/>
              </svg>
            </button>
          </div>
          
          <div class="layer-content">
            <span class="object-type-icon">
              {{ getObjectTypeIcon(layer.objects[0]) }}
            </span>
            <span class="layer-name">{{ layer.name }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LayerPanel',
  props: {
    layers: {
      type: Array,
      required: true
    },
    activeLayerId: {
      type: String,
      default: null
    }
  },
  methods: {
    getObjectTypeIcon(obj) {
      if (!obj) return '⚪';
      switch (obj.type) {
        case 'rect':
          return '⬜';
        case 'circle':
          return '⭕';
        case 'triangle':
          return '▲';
        case 'polygon':
          return '⬡';
        case 'line':
          return '╱';
        case 'i-text':
        case 'text':
        case 'textbox':
          return 'T';
        case 'image':
          return '🖼️';
        default:
          return '⚪';
      }
    }
  }
};
</script>

<style scoped>
.layer-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #f0f2f5;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
}

.layer-header {
  padding: 8px 12px;
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
}

.layer-title {
  font-size: 14px;
  font-weight: 500;
  color: #1f1f1f;
}

.layer-actions {
  display: flex;
  gap: 4px;
}

.action-btn {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: #595959;
  cursor: pointer;
  border-radius: 3px;
  font-size: 14px;
}

.action-btn:hover {
  background: #f5f5f5;
  color: #1890ff;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.layer-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px;
}

.layer-item {
  margin-bottom: 1px;
  cursor: pointer;
  user-select: none;
}

.layer-item:hover {
  background: #f5f5f5;
}

.layer-item.active {
  background: #e6f7ff;
}

.layer-item-header {
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.layer-controls {
  display: flex;
  gap: 2px;
  min-width: 44px;
}

.visibility-toggle,
.lock-toggle {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
  color: #595959;
  border-radius: 3px;
}

.visibility-toggle:hover,
.lock-toggle:hover {
  background: #f0f0f0;
}

.icon {
  font-size: 12px;
  line-height: 1;
  fill: #595959;
  transition: fill 0.2s ease;
}

.icon.locked,
.icon.visible {
  fill: #1890ff;
}

.visibility-toggle:hover .icon,
.lock-toggle:hover .icon {
  fill: #1890ff;
}

.layer-content {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 4px;
  min-width: 0;
}

.object-type-icon {
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #595959;
}

.layer-name {
  flex: 1;
  font-size: 12px;
  color: #262626;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style> 